package com.grgbanking.it.project.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grgbanking.it.common.entity.PageQuery;
import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.project.entity.OperationWork;
import com.grgbanking.it.project.entity.OperationWorkHandle;
import com.grgbanking.it.project.service.IOperationWorkService;
import com.grgbanking.it.system.entity.User;

/**
 * 运维工作控制器
 * @author pgwei
 *
 */
@Controller
@RequestMapping("/project")
public class OperationWorkController {
	@Autowired
	private IOperationWorkService operationWorkService;

	/**
	 * 编辑工作
	 * @param operationWork
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork",method=RequestMethod.POST)
    public Object saveOrUpdate(HttpSession httpSession,OperationWork operationWork){  
		String json = null;
        try {
        	/**
        	 * 暂时只做保存功能，更新后期再做
        	 */
        	User user = (User) httpSession.getAttribute("loginUser");
        	operationWork.setCreator(user.getUserId());
        	operationWorkService.saveOrUpdate(operationWork);
        	json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
        return json;  
    }   
	
	/**
	 * 从OA过来新的工作
	 * @param operationWork
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork/oa",method=RequestMethod.POST)
	public Object newFormOA(OperationWork operationWork){  
		String json = null;
		try {
			operationWork.setWorkOrigin("OA");
			operationWork.setMsgType(2);
			operationWorkService.saveOrUpdate(operationWork);
			json = "报修成功!";
		} catch (Exception e) {
			json = "报修失败!";
			e.printStackTrace();
		}  
		return json;  
	}   
	
	/**
	 * 查询工作
	 * @param operationWork
	 * @param start
	 * @param limit
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork",method=RequestMethod.GET)
	public Object search(HttpSession httpSession,OperationWork operationWork,PageQuery query){  
		String json = null;
		try {
			User user = (User) httpSession.getAttribute("loginUser");
			json = operationWorkService.findByPageToJson(query, operationWork,user);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	/**
	 * 获取单个工作
	 * @param operationWork
	 * @param start
	 * @param limit
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork/{id}",method=RequestMethod.GET)
	public Object get(@PathVariable("id") Integer id){  
		String json = null;
		try {
			json = operationWorkService.findByIdToJson(id);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}  
	
	/**
	 * 删除工作
	 * delete
	 * 
	 * @param id
	 * @param httpSession
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork/{id}",method=RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Integer id,HttpSession httpSession){  
		String json = null;
		try {
			User user = (User) httpSession.getAttribute("loginUser");
			operationWorkService.delete(user, id);
			json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		
		return json;  
	}  
	
	/**
	 * 简单统计
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork/count",method=RequestMethod.GET)
	public Object count(HttpSession httpSession){  
		String json = null;
		try {
			User user = (User) httpSession.getAttribute("loginUser");
			json = operationWorkService.getSimpleStatistics(user);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   

	/**
	 * 分配工作
	 * @param workId 工作编号
	 * @param handlerJson 被分配人的json数据
	 * @return
	 */
	@ResponseBody  
	@RequestMapping("operationWork/distribute")
	public Object distribute(Integer workId,String handlerJson){  
		String json = null;
		try {
			operationWorkService.updateForDistribute(workId, handlerJson);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	
	/**
	 * 处理工作
	 * @param workId 工作编号
	 * @param handlerJson 被分配人的json数据
	 * @return
	 */
	@ResponseBody  
	@RequestMapping("operationWork/handle")
	public Object handle(HttpSession httpSession,Integer workId,String describe,OperationWorkHandle operationWorkHandle){  
		String json = null;
		try {
			User handler = (User) httpSession.getAttribute("loginUser");
			operationWorkService.updateForHandle(workId, describe, operationWorkHandle, handler);
			json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	
	/**
	 * 工作的所有处理
	 * @param workId
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="operationWork/handles",method=RequestMethod.GET)
	public Object searchHandles(Integer workId){  
		String json = null;
		try {
			json = operationWorkService.findHandleToJson(workId);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	/**
	 * 导出数据
	 * export
	 * 
	 * @param response
	 * @param operationWork
	 * @param exportParams 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="operationWork/export",method=RequestMethod.GET)
    public void export(HttpSession httpSession,HttpServletResponse response,OperationWork operationWork,String exportParams){  
        try {
        	User user = (User) httpSession.getAttribute("loginUser");
			HSSFWorkbook book = operationWorkService.getExtportBook(operationWork, user, exportParams);
			// 组装附件名称和格式
			response.setHeader("Content-disposition", "attachment;filename=OperationWork.xls");  
			book.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}  
    }   
}
